package com.xiangban.yanglaodajie_platform.controller;

import com.xiangban.yanglaodajie_platform.aop.Permission;
import com.xiangban.yanglaodajie_platform.entity.dto.LoginUserDto;
import com.xiangban.yanglaodajie_platform.entity.dto.UserDto;
import com.xiangban.yanglaodajie_platform.entity.res.ApiResult;
import com.xiangban.yanglaodajie_platform.entity.res.ApiResultCode;
import com.xiangban.yanglaodajie_platform.entity.vo.UserApiVo;
import com.xiangban.yanglaodajie_platform.entity.vo.UserVo;
import com.xiangban.yanglaodajie_platform.exception.BizException;
import com.xiangban.yanglaodajie_platform.service.UserService;
import com.xiangban.yanglaodajie_platform.utils.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import static com.xiangban.yanglaodajie_platform.utils.CommonUtils.BizEmpty;

/**
 * @Author Mr. MaYaYun
 * @Data 2021/2/3 9:09
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
@Api(tags = "（软件）用户控制台")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/registerUser")
    @ApiOperation("用户注册")
    public ApiResult<Object> registerUser(@RequestBody UserVo user) {
        userService.register(user);
        return ApiResult.newSuccess();
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public ApiResult<Object> login(@RequestBody LoginUserDto loginUserDto) {
        return ApiResult.newSuccess(userService.login(loginUserDto));
    }

    @PostMapping("/shopLogin")
    @ApiOperation("商家登陆")
    public ApiResult<Object> shopLogin(@RequestBody LoginUserDto loginUserDto) {
        return ApiResult.newSuccess(userService.shopLogin(loginUserDto));
    }

    @PostMapping("/resetPassword")
    @ApiOperation("忘记密码")
    public ApiResult<Object> resetPassword(@RequestBody LoginUserDto loginUserDto) {
        userService.resetPassword(loginUserDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/resetShopPassword")
    @ApiOperation("商家忘记密码")
    public ApiResult<Object> resetShopPassword(@RequestBody LoginUserDto loginUserDto) {
        userService.resetShopPassword(loginUserDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/getVerificationCode")
    @ApiOperation("获取验证码")
    public ApiResult<Object> verification(@RequestBody LoginUserDto loginUserDto) {
        userService.verification(loginUserDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/logout")
    @ApiOperation("注销")
    public ApiResult<Object> logout() {
        userService.logout();
        log.info("用户注销");
        return ApiResult.newSuccess();
    }

    @PostMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public ApiResult<Object> getUserInfo() {
        UserDto user = userService.getUserInfo();
        return ApiResult.newSuccess(user);
    }

    @PostMapping("/loadRegisterUser")
    @ApiOperation("注册选项")
    public JsonData loadRegisterUser(@RequestBody Map<String, Integer> map) {
        return JsonData.buildSuccess(userService.registerInfo(map), "操作成功");
    }

    @PostMapping("/updateUserInfo")
    @ApiOperation("修改用户信息")
//    @Permission
    public ApiResult<Object> updateUserInfo(@RequestBody UserDto userDto) {
        userService.updateUserInfo(userDto);
        return ApiResult.newSuccess();
    }

    @PostMapping("/checkLogin")
    @ApiOperation("检查用户是否登陆")
    @Permission
    public ApiResult<Object> checkLogin() {
        return ApiResult.newSuccess();
    }

    @PostMapping("/checkShopLogin")
    @ApiOperation("检查商家用户是否登陆")
    @Permission(type = "shop")
    public ApiResult<Object> checkShopLogin() {
        return ApiResult.newSuccess();
    }



    //**************************平台交互***********************************
    @PostMapping("/addSave")
    @ApiOperation("用户添加（交互接口）")
    @Permission
    public ApiResult<Object> addUser(@RequestBody UserApiVo user){
        userService.addUser(user);
        return ApiResult.newSuccess(user.getUserId());
    }

    @PostMapping("/updateUser")
    @ApiOperation("用户信息修改（交互接口）")
    @Permission
    public ApiResult<Object> updateUser(@RequestBody UserApiVo user){
        userService.updateUser(user);
        return ApiResult.newSuccess();
    }

    @PostMapping("/updateAccumulatePoints")
    @ApiOperation("用户积分修改（交互接口）")
    @Permission
    public ApiResult<Object> updateAccumulatePoints(@RequestBody UserApiVo user){
        if (BizEmpty(user.getMobile())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(user.getEatPoint())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        if (BizEmpty(user.getLeaderId())){
            throw new BizException(ApiResultCode.PARAMETER_DEFICIENCY);
        }
        try {
            userService.updateAccumulatePoints(user);
        } catch (Exception e) {
            throw new BizException(ApiResultCode.SYS_ERROR);
        }
        return ApiResult.newSuccess();
    }





}
